//
//  LZMyCenterViewController.m
//  laziz_Merchant
//
//  Created by ZhaoDongBo on 2016/11/24.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZMyCenterViewController.h"
#import "LZMyPersonInfoViewController.h"
#import "LZStaffInfoViewController.h"
#import "LZSettingsViewController.h"
#import "LZMyCenterTableView.h"

#import "GVUserDefaults+LZProperties.h"
#import "UIViewController+NavigationItemSetting.h"
#import "UIView+Rotation.h"

#import <MOBFoundation/MOBFoundation.h>
#import <ShareSDK/ShareSDK.h>
#import "TShareEventHandler.h"
#import "TShareView.h"
#import "Masonry.h"

#import "LZUserInfoPresenter.h"
#import "LZActionSheet.h"
#import "LZCommonWebViewController.h"
#import "LZRemindView.h"
#import "LZUserModel.h"
#import "AppDelegate.h"

extern NSString *merchant;

@interface LZMyCenterViewController ()

@property (nonatomic, strong) LZMyCenterTableView   * tableView;
@property (nonatomic, strong) TShareView            * shareView;

@property (nonatomic, strong) UIButton *nav_left_back;
@property (assign, nonatomic) CGFloat lzAlpha;

@property (nonatomic, strong) LZUserInfoPresenter   * userInfoPresenter;
@property (nonatomic, weak) LZActionSheet         * sheetView;

@end

@implementation LZMyCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.umLogPageViewName = @"个人中心页面";
    self.view.backgroundColor = UIColorFromRGB(0xffffff, 1);
    self.lzAlpha = 0;
    self.userInfoPresenter = [[LZUserInfoPresenter alloc] init];
    
    [self configView];
    [self addAutoLayout];
    [self tableViewAction];
    [self configNavigationBar];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)fd_prefersNavigationBarHidden {
    return YES;
}


- (void)configView {
    [self.view addSubview:self.tableView];
}

- (void)addAutoLayout{
    
    [self.tableView makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.leading.mas_equalTo(0);
        make.trailing.mas_equalTo(0);
    }];
}

//配置navi
- (void)configNavigationBar{
    
    self.nav_left_back = [UIButton buttonWithType:UIButtonTypeCustom];
    self.nav_left_back.frame = CGRectMake(0, 0, 60, 40);
    [self.nav_left_back addTarget:self action:@selector(navbarBackButtonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    UIImage *image_back = [UIImage imageNamed:@"lz_back_en"];
    [self.nav_left_back setImage:image_back forState:UIControlStateNormal];
    self.nav_left_back.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, CGRectGetWidth(self.nav_left_back.bounds)-image_back.size.width);
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0) {
        [self.nav_left_back setLanguageRotaion];
    }
    
    [self hiddenBarNavigationItemSetting:@[self.nav_left_back] type:NAVIGATIONITEMSETTING_LEFT];
    [self lz_setCustomBarAlpha:self.lzAlpha];
}

- (void)navbarBackButtonClickAction:(UIButton*)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)tableViewAction {
    
    WS(weakSelf);
    
    self.tableView.didButtonActionBlock = ^(UIButton * button) {
        DLog(@"注销登陆");
        //取消保存
        [[LZUserModel Instance] logOut];
        
        [weakSelf.navigationController popToRootViewControllerAnimated:YES];
        
        AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
        [appDelegate setRootForLoginVCWithIsShowLeadingView:NO];
    };

    
    self.tableView.didSelectRowBlock = ^(NSIndexPath * indexPath) {
        
        if (indexPath.section == 0) {
            
            if ([[GVUserDefaults standardUserDefaults].userType integerValue] != kUserType_manmager) {
                LZStaffInfoViewController * vc = [[LZStaffInfoViewController alloc] init];
                [weakSelf.navigationController pushViewController:vc animated:YES];
                
            }else{
                LZMyPersonInfoViewController * vc = [[LZMyPersonInfoViewController alloc] init];
                [weakSelf.navigationController pushViewController:vc animated:YES];
            }
        }
        else if (indexPath.section == 1) {
            
            switch (indexPath.row) {
                case 0:{//联系客服
                    [weakSelf.userInfoPresenter userGetServiceTelSuccess:^(id data) {
                        DLog(@"获取服务电话成功: %@", data);
                        [weakSelf showSheetView:[data objectForKey:@"data"]];
                    } onFail:^(NSString *fail) {
                        DLog(@"获取服务电话失败: %@", fail);
                    }];
                }
                    break;
                case 1:{//评价
                    [TShareEventHandler jumpToAppstoreForMerchantApp];
                }
                    break;
                case 2:{//分享
                    [weakSelf.shareView shareViewOpen];
                    weakSelf.shareView.shareViewButtonActionBlock = ^(TSHAREVIEW_BUTTON_TYPE type){
                        [weakSelf shareAction:type];
                    };
                }
                    break;
                case 3:{//设置
                    
                    LZSettingsViewController *settingsVC = [[LZSettingsViewController alloc]initWithNibName:@"LZSettingsViewController" bundle:nil];
                    [weakSelf.navigationController pushViewController:settingsVC animated:YES];
                    
                }
                    break;
                case 4:{//关于我们
                    // 关于我们
                    LZCommonWebViewController *aboutUsVc = [[LZCommonWebViewController alloc]init];
                    aboutUsVc.webCode = kWebState_aboutUs;
                    [weakSelf.navigationController pushViewController:aboutUsVc animated:YES];
                }
                    break;
                    
                default:
                    break;
            }
        }
    };
}

- (void)showSheetView:(NSString*)phoneNumber {
    
    [self.sheetView ReloadTableViewArrayTitle:@[@[phoneNumber],@[LZLocalizedString(@"cancel", nil)]]];
    [self.sheetView ShowView];
}

- (void)callPhone:(NSString*)phoneNumber {
    NSURL *phoneURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",[phoneNumber stringByReplacingOccurrencesOfString:@" " withString:@""]]];
    [[UIApplication sharedApplication] openURL:phoneURL];
}

- (void)shareAction:(NSInteger)type {
    
    /* 分享APP */
    SSDKPlatformType sharePlatformType = SSDKPlatformTypeUnknown;
    NSArray * imageArray;
    
    switch (type) {
        case 0:
            sharePlatformType = SSDKPlatformTypeSMS;
            break;
        case 1:
            sharePlatformType = SSDKPlatformTypeMail;
            break;
        case 2:
        {
            sharePlatformType = SSDKPlatformTypeFacebook;

            NSString *imageName = @"default/laziz_m_en.png";
            if ([[LZLanguageManager shareInstance] getLanguageType] == LanguageType_Ar) {
                imageName = @"default/laziz_m_ar.png";
            }
            
            imageArray = @[[NSString stringWithFormat:@"%@%@", APIBaseImageUrl, imageName]];
        }
            break;
        case 3:
        {
            sharePlatformType = SSDKPlatformTypeTwitter;
            
            NSString *imageName = @"laziz_merchant_en";
            if ([[LZLanguageManager shareInstance] getLanguageType] == LanguageType_Ar) {
                imageName = @"laziz_merchant_ar";
            }
            
            imageArray = @[[UIImage imageNamed:imageName]];
        }
            break;
    }
    
    [TShareEventHandler shareWithShareURL:[NSString stringWithFormat:@"%@%@%@", APIBaseURL, merchant, @"/dist/r.html"]
                            shareImageUrl:imageArray
                               shareTitle:LZLocalizedString(@"share_app_title", nil)
                                shareText:LZLocalizedString(@"share_content", nil)
                                shareType:sharePlatformType
                                  topView:nil
                               stateBlock:^(SSDKResponseState state) {
                                   
                                   if (state == SSDKResponseStateSuccess) {
                                       [LZRemindView initWithImage:[UIImage imageNamed:@"提示对勾"] andContent:LZLocalizedString(@"share_success", nil)];
                                   }
                                   else if (state == SSDKResponseStateFail) {
                                       [LZRemindView initWithImage:[UIImage imageNamed:@"错误提示"] andContent:LZLocalizedString(@"share_fail", nil)];
                                   }
                                   else if (state == SSDKResponseStateCancel) {
                                       [LZRemindView initWithImage:[UIImage imageNamed:@"错误提示"] andContent:LZLocalizedString(@"share_fail", nil)];
                                   }
                               }];
    
    [self.shareView shareViewClose];
}

- (LZMyCenterTableView *)tableView {
    
    if (_tableView == nil) {
        _tableView = [[LZMyCenterTableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.dataArrays = @[LZLocalizedString(@"contact_customer", nil),
                                  LZLocalizedString(@"rate_app", nil),
                                  LZLocalizedString(@"share_app", nil),
                                  LZLocalizedString(@"setting", nil),
                                  LZLocalizedString(@"about_us", nil)];
    }
    
    return _tableView;
}

- (TShareView *)shareView {
    
    if (_shareView == nil) {
        _shareView = [[TShareView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    }
    
    return _shareView;
}

- (LZActionSheet*)sheetView {
    if (_sheetView) {
        return _sheetView;
    }
    
    LZActionSheet *sheet = [[LZActionSheet alloc] initWithFrame:self.navigationController.view.bounds];
    _sheetView = sheet;
    [self.navigationController.view addSubview:_sheetView];
    
    WS(weakSelf)
    self.sheetView.ClickSheetBlock = ^(NSIndexPath *indexPath, id obj) {
        if (indexPath.section == 0) {
            [weakSelf callPhone:obj];
        }
    };
    
    return _sheetView;
}


@end
